//=======================================================================================
//= EventManager.cpp
//=======================================================================================
#include "StdAfx.h"

//------------------------------------------------------
//- Variables
//------------------------------------------------------
cEventManager EventManager;
xEventList EventList[255];

//------------------------------------------------------
//- Start
//------------------------------------------------------
void cEventManager::Load()
{
	//--------------------------------------------------------------------------------
	FILE *fp;
	char sLineTxt[255] = {0};
	//--------------------------------------------------------------------------------
	this->Count = 0;
	//--------------------------------------------------------------------------------
	fp = fopen(INI_EVENT_MAN,"r");
	//--------------------------------------------------------------------------------
	if(!fp)
	{
		Tools.FileExist(INI_EVENT_MAN);
	}
	//--------------------------------------------------------------------------------
	rewind(fp);
	//--------------------------------------------------------------------------------
	while(fgets(sLineTxt, 255, fp) != NULL)
	{
		//--------------------------------------------------------------------------------
		if(sLineTxt[0] == '/')continue;
		if(sLineTxt[0] == ';')continue;
		//--------------------------------------------------------------------------------
		sscanf(sLineTxt, "%d %d %d %d %d %d", &this->Value[0],&this->Value[1],&this->Value[2],&this->Value[3],&this->Value[4],&this->Value[5]);
		//--------------------------------------------------------------------------------
		EventList[this->Count].Type		= this->Value[0];
		EventList[this->Count].Day		= this->Value[1];
		EventList[this->Count].Month	= this->Value[2];
		EventList[this->Count].Year		= this->Value[3];
		EventList[this->Count].Hour		= this->Value[4];
		EventList[this->Count].Minute	= this->Value[5];
		//--------------------------------------------------------------------------------
		this->Count++;
		//--------------------------------------------------------------------------------
	}
	//--------------------------------------------------------------------------------
	rewind(fp);
	fclose(fp);
	//--------------------------------------------------------------------------------
	Console.Write("[File] File %s loaded!",INI_EVENT_MAN);
	//--------------------------------------------------------------------------------
}
void EventsManage(void * lpParam)
{
	Console.Write("[EventManager] Event Manager thread has been started!");
	//--------------------------------------------------------------------------------
	while(true)
	{
		//--------------------------------------------------------------------------------
		SYSTEMTIME t;
		GetLocalTime(&t);
		//--------------------------------------------------------------------------------
		for(int x=0;x < EventManager.Count;x++)
		{
			//--------------------------------------------------------------------------------
			DWORD CurrentTime = MAKELONG(t.wHour,t.wMinute);
			DWORD MessageTime = MAKELONG(EventList[x].Hour, EventList[x].Minute);
			//--------------------------------------------------------------------------------
			if(MessageTime == CurrentTime)
			{
				if(EventList[x].Day == t.wDay && EventList[x].Month == t.wMonth && EventList[x].Year == t.wYear)
				{
					//--------------------------------------------------------------------------------
					switch(EventList[x].Type)
					{
						//--------------------------------------------------------------------------------
						case HappyHour:
							//--------------------------------------------------------------------------------
							Console.Write("[EventManager] Happy Hour event is started!");
							//--------------------------------------------------------------------------------
							EventManager.HappyHourStarted = 0;
							//--------------------------------------------------------------------------------
							Launch(HappyHourEvent);
							//--------------------------------------------------------------------------------
						break;
						//--------------------------------------------------------------------------------
					}
					//--------------------------------------------------------------------------------
				}
				//--------------------------------------------------------------------------------
				Sleep(60000); //Sleep 1min to prevent to repeat it self!
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
		}
		//--------------------------------------------------------------------------------
	}
	//--------------------------------------------------------------------------------
	_endthread();
	//--------------------------------------------------------------------------------
}
void cEventManager::Start()
{
	if(ReadINI.GetInt("Main","EventManagerEnabled",INI) == 1)
	{
		this->Load();
		_beginthread(EventsManage,0,NULL);
	}
}
void cEventManager::EditExp(char Exp[256])
{
    //-------------------------------------------------------
    float * EXPERIENCE = (float *)(0x7A81A8);
    (*EXPERIENCE)= (float)atof(Exp);
    //-------------------------------------------------------
}
void cEventManager::EditDrop(int Drop)
{
    //-------------------------------------------------------
    __asm
    {
        //-------------------------------------------------------
        MOV EAX,Drop
        MOV DWORD PTR DS:[0x007A8170],EAX
        //-------------------------------------------------------
    }
    //-------------------------------------------------------
}
void HappyHourEvent(void * lpParam)
{
	//--------------------------------------------------------------------------------
	char Exp[256];
	//--------------------------------------------------------------------------------
	Tools.FileExist(INI_EVENTS);
	//--------------------------------------------------------------------------------
	ReadINI.GetString(Exp,"HappyHour","HappyHour_ExpRate",INI_EVENTS);
	//--------------------------------------------------------------------------------
	if(ReadINI.GetInt("HappyHour","HappyHour_Enabled",INI_EVENTS)==1) 
	{	
		//--------------------------------------------------------------------------------
		if(EventManager.HappyHourStarted == 1)
		{
			//--------------------------------------------------------------------------------
			return;
			//--------------------------------------------------------------------------------
		}
		//--------------------------------------------------------------------------------
		Player.SendAllTextMsg(0,"Happy Hour event is started!");
		//--------------------------------------------------------------------------------
		EventManager.EditDrop(ReadINI.GetInt("HappyHour","HappyHour_DropRate",INI_EVENTS));
		EventManager.EditExp(Exp);
		//--------------------------------------------------------------------------------
		Player.SendAllTextMsg(0,"Current Exp Rate: %s / Current Drop Rate: %s",Exp,ReadINI.GetInt("HappyHour","HappyHour_DropRate",INI_EVENTS));
		//--------------------------------------------------------------------------------
		EventManager.HappyHourStarted = 1;
		//--------------------------------------------------------------------------------
		Sleep((ReadINI.GetInt("HappyHour","HappyHour_Time",INI_EVENTS)*60000));
		//--------------------------------------------------------------------------------
		EventManager.HappyHourStarted = 0;
		//--------------------------------------------------------------------------------
		Player.SendAllTextMsg(0,"Happy Hour event is finished!");
		//--------------------------------------------------------------------------------
		GetPrivateProfileString("GameServerInfo","ExperienceRate","200",Exp,sizeof(Exp),"..\\data\\CommonServer.cfg");
		//--------------------------------------------------------------------------------
		EventManager.EditDrop(GetPrivateProfileInt("GameServerInfo", "ItemDropRate", 20, "..\\data\\CommonServer.cfg"));
		EventManager.EditExp(Exp);
		//--------------------------------------------------------------------------------
		_endthread();
		//--------------------------------------------------------------------------------
	}
}
//------------------------------------------------------
//- Variables
//------------------------------------------------------
extern cEventManager EventManager;